const mysql = require('mysql2/promise');
const dbConfig = require('../config/db.config.js');

const pool = mysql.createPool({
    host: dbConfig.host,
    user: dbConfig.user,
    password: dbConfig.password,
    database: dbConfig.database
});

const getMovies = async () => {
    try {
        const [rows] = await pool.execute('SELECT * FROM movies');
        return rows;
    } catch (error) {
        console.error('Error fetching movies:', error);
        throw error;
    }
};

const createMovie = async (title, description, duration, release_date) => {
    try {
        const [result] = await pool.execute(
            'INSERT INTO movies (title, description, duration, release_date) VALUES (?,?,?,?)',
            [title, description, duration, release_date]
        );
        return result.insertId;
    } catch (error) {
        console.error('Error creating movie:', error);
        throw error;
    }
};

module.exports = {
    getMovies,
    createMovie
};